package cn.iocoder.yudao.module.book.service.homeworks;

import cn.iocoder.yudao.module.book.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.book.controller.admin.homeworks.vo.*;
import cn.iocoder.yudao.module.book.controller.app.homeworks.vo.AppHomeworksRespVO;
import cn.iocoder.yudao.module.book.controller.app.homeworks.vo.AppHomeworkDetailRespVO;
import cn.iocoder.yudao.module.book.dal.dataobject.homeworks.HomeworksDO;
import cn.iocoder.yudao.module.book.dal.dataobject.homeworks.HomeworkCollectionsDO;
import cn.iocoder.yudao.module.book.dal.mysql.homeworks.HomeworkCollectionsMapper;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.book.dal.mysql.homeworks.HomeworksMapper;
import cn.iocoder.yudao.module.book.dal.mysql.classes.ClassesMapper;
import cn.iocoder.yudao.module.book.dal.dataobject.classes.ClassesDO;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.book.enums.ErrorCodeConstants.*;

/**
 * 作业 Service 实现类
 *
 * @author xi
 */
@Service
@Validated
public class HomeworksServiceImpl implements HomeworksService {

    @Resource
    private HomeworksMapper homeworksMapper;
    @Resource
    private ClassesMapper classesMapper;
    @Resource
    private HomeworkCollectionsMapper homeworkCollectionsMapper;

    @Override
    public Boolean isClassTeacher(Integer classesId, Long userId) {
        ClassesDO classesDO = classesMapper.selectById(classesId);
        if (classesDO == null) {
            throw exception(ErrorCodeConstants.BANJI_NOT_EXISTS);
        }
        return userId.equals(Long.valueOf(classesDO.getUserId()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer createHomeworks(HomeworksSaveReqVO createReqVO) {
        // 验证班级是否存在
        ClassesDO classesDO = classesMapper.selectById(createReqVO.getClassesId());
        if (classesDO == null) {
            throw exception(ErrorCodeConstants.BANJI_NOT_EXISTS);
        }

        // 创建作业对象
        HomeworksDO homework = BeanUtils.toBean(createReqVO, HomeworksDO.class);
        homework.setCreateTime(LocalDateTime.now());
        homework.setUpdateTime(LocalDateTime.now());
        homework.setDeleted(false);

        // 保存到数据库
        homeworksMapper.insert(homework);

        // 插入作业-绘本集关联关系
        if (createReqVO.getCollectionIds() != null && !createReqVO.getCollectionIds().isEmpty()) {
            List<HomeworkCollectionsDO> homeworkCollections = new ArrayList<>();
            for (Integer collectionId : createReqVO.getCollectionIds()) {
                HomeworkCollectionsDO homeworkCollection = new HomeworkCollectionsDO();
                homeworkCollection.setHomeworkId(homework.getHomeworkId());
                homeworkCollection.setCollectionId(collectionId);
                homeworkCollections.add(homeworkCollection);
            }
            // 批量插入关联关系
            homeworkCollectionsMapper.insertBatch(homeworkCollections);
        }

        return homework.getHomeworkId();
    }

    @Override
    public void updateHomeworks(HomeworksSaveReqVO updateReqVO) {
        // 校验存在
        validateHomeworksExists(updateReqVO.getHomeworkId());
        // 更新
        HomeworksDO updateObj = BeanUtils.toBean(updateReqVO, HomeworksDO.class);
        homeworksMapper.updateById(updateObj);
    }

    @Override
    public void deleteHomeworks(Integer id) {
        // 校验存在
        validateHomeworksExists(id);
        // 删除
        homeworksMapper.deleteById(id);
    }

    private void validateHomeworksExists(Integer id) {
        if (homeworksMapper.selectById(id) == null) {
            throw exception(HOMEWORKS_NOT_EXISTS);
        }
    }

    @Override
    public HomeworksDO getHomeworks(Integer id) {
        return homeworksMapper.selectById(id);
    }

    @Override
    public PageResult<HomeworksDO> getHomeworksPage(HomeworksPageReqVO pageReqVO) {
        return homeworksMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AppHomeworksRespVO> getClassHomeworks(Integer classId, Long userId) {
        // 验证班级是否存在
        ClassesDO classesDO = classesMapper.selectById(classId);
        if (classesDO == null) {
            throw exception(ErrorCodeConstants.BANJI_NOT_EXISTS);
        }

        // 获取班级所有作业
        List<HomeworksDO> homeworks = homeworksMapper.selectByClassId(classId);

        // 转换为响应VO
        return BeanUtils.toBean(homeworks, AppHomeworksRespVO.class);
    }

    @Override
    public AppHomeworkDetailRespVO getHomeworkDetail(Integer id) {
        // 查询作业详情及关联的绘本集
        AppHomeworkDetailRespVO homeworkDetail = homeworksMapper.selectHomeworkWithCollections(id);
        if (homeworkDetail == null) {
            throw exception(HOMEWORKS_NOT_EXISTS);
        }

        return homeworkDetail;
    }

}